package com.exam.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exam.api.common.BaseController;
import com.exam.api.common.ReturnMsg;
import com.exam.api.dto.QuestionsDTO;
import com.exam.api.service.QuestionsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.exam.api.common.BaseSelectEntity;

import java.util.ArrayList;

import com.exam.api.service.ExamService;
import com.exam.api.dto.ExamDTO;

import javax.annotation.Resource;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import com.exam.api.common.BaseSelectEntity;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Date;

/**
 * @description: 试题信息对外接口层
 * @author: wuming
 * @create: 2024.05.03 13:24:03
 */
@RestController
@RequestMapping("/questions")
public class QuestionsController extends BaseController<QuestionsService, QuestionsDTO> {

    @Autowired
    private ExamService examService;

    @Override
    @Resource(name = "questionsService")
    protected void setBaseService(QuestionsService questionsService) {
        this.baseService = questionsService;
    }

    /**
     * 默认分页查询
     *
     * @param questionsDTO
     * @return
     */
    @PostMapping("/page")
    public ReturnMsg page(@RequestBody QuestionsDTO questionsDTO) {
        return ReturnMsg.ok(baseService.basePage(questionsDTO));
    }

    /**
     * 根据XML中的sql语句进行查询
     *
     * @param questionsDTO
     * @return
     */
    @PostMapping(value = "/findList")
    public ReturnMsg findList(@RequestBody QuestionsDTO questionsDTO) {
        return ReturnMsg.ok(baseService.findList(questionsDTO));
    }

    @Override
    protected void beforeSave(QuestionsDTO entity) throws Exception {
        entity.setCreateTime(new Date());
        super.beforeSave(entity);
    }

    @GetMapping("/all/exam")
    public ReturnMsg allExam(@RequestParam(required = false) Long teacherId) {
        List<BaseSelectEntity> list = new ArrayList<>();
        LambdaQueryWrapper<ExamDTO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (teacherId != null) {
            lambdaQueryWrapper.eq(ExamDTO::getTeacherId, teacherId);
        }
        List<ExamDTO> dtos = examService.list(lambdaQueryWrapper);
        for (ExamDTO dto : dtos) {
            BaseSelectEntity baseSelectEntity = new BaseSelectEntity();
            baseSelectEntity.setKey(dto.getId());
            baseSelectEntity.setValue(dto.getExamName());
            list.add(baseSelectEntity);
        }
        return ReturnMsg.ok(list);
    }

}

